package com.pan.code.impl;

import com.pan.code.VerificationCode;
import com.pan.entity.Base64Result;
import com.pan.entity.Compute;
import com.pan.enumn.ComputeEnum;
import com.pan.picture.Picture;
import com.pan.picture.impl.CodePicture;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Random;

public class ComputeVerificationCode implements VerificationCode {

    private Picture picture;

    private List<Compute> computeList;

    private Random random;

    public ComputeVerificationCode() {
        picture = new CodePicture();
        computeList = new ArrayList<>();
        computeList.add(new Compute(ComputeEnum.ADD_SIGN));
        computeList.add(new Compute(ComputeEnum.SUBTRACT_SIGN));
        computeList.add(new Compute(ComputeEnum.MULTIPLY_SIGN));

    }

    @Override
    public Base64Result getBase64() {
        int num1 = (int) (Math.random()*90) + 10;
        int num2 = (int) (Math.random()*90) + 10;
        int size = computeList.size();
        Compute compute = computeList.get((int) (Math.random() * size));
        String str  = String.valueOf(num1 )+String.valueOf(compute.getSign())+String.valueOf( num2);
        int result = compute.getCalculate().apply(num1,num2);
        picture.reset(str);
        picture.interfere();
        BufferedImage image = picture.getImage();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
        try {
            ImageIO.write(image, "jpg", baos);//写入流中
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = baos.toByteArray();//转换成字节
        Base64.Encoder encoder = Base64.getEncoder();
        String jpg_base64 = encoder.encodeToString(bytes).trim();//转换成base64串
        jpg_base64 = jpg_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
        String base64 = "data:image/jpg;base64," + jpg_base64;
        return new Base64Result(String.valueOf(result),base64);
    }

    public void addCompute(Compute... computes){
        for (Compute compute : computes){
            computeList.add(compute);
        }
    }

    public void resetCompute(List<Compute> computes){
        if (computes.size() == 0) throw new RuntimeException("computes 不能为 0 ");
        computeList = computes;
    }

    @Override
    public String get(String filePath, String fileName) {
        int num1 = (int) (Math.random()*90) + 10;
        int num2 = (int) (Math.random()*90) + 10;
        int size = computeList.size();
        Compute compute = computeList.get((int) (Math.random() * size));
        String str  = String.valueOf(num1 )+String.valueOf(compute.getSign())+String.valueOf( num2);
        int result = compute.getCalculate().apply(num1,num2);
        picture.reset(str);
        picture.interfere();
        BufferedImage image = picture.getImage();
        File dir = new File(filePath);
        if (!dir.exists()) throw new RuntimeException("filePath: "+filePath+" 不存在");
        File outFile  = new File(filePath+"\\"+fileName+".jpg");
        if (outFile.exists()) throw new RuntimeException(filePath+"\\"+fileName+".jpg"+"该图片已存在");
        try {
            outFile.createNewFile();
            ImageIO.write(image,"jpg",outFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return String.valueOf(result);
    }

    @Override
    public void close() {
        picture.close();
    }
}
